Page({
  data: {
    
  },
  onLoad: function (options) {
    
  },
  onShow: function () {
    let cartList = wx.getStorageSync('cart') || []
    this.setData({
      cartList:cartList
    })
    this.getTotal()
  },
  //计算总价格和总数量
  getTotal(){
    let cartList = this.data.cartList
    let totalMoney = 0
    let totalNum = 0
    cartList.forEach(item=>{
      totalMoney+=item.price*item.num
      totalNum+=item.num
    })
    this.setData({
      totalMoney,
      totalNum
    })
  },
  //提交订单
  submit(){
    //  console.log('提交了订单');
    let user = wx.getStorageSync('user_info_cache')
    console.log('user_info_cache:',user);
    if(!user){
      wx.showToast({
        title: '请先登录',
        icon:'error'
      })
      setTimeout(()=>{
        wx.switchTab({
          url: '/pages/person/person',
        })
      },1000)
      
    }
    wx.cloud.database().collection('order')
      .add({
        data:{
          name: user.nickName,
          status: 0, //-1订单取消,0新下单待上餐,1待用户评价,2订单已完成
          beizhu: this.data.beizhu,
          orderList:this.data.cartList,
          totalPrice:this.data.totalMoney,
          time:getApp().getTime()
        }
      }).then(res=>{
        console.log('提交订单成功！',res);
        wx.setStorageSync('cart', null);//清空购物车缓存
        wx.showToast({
          title: '下单成功！',
        })
        setTimeout(()=>{
          wx.switchTab({
            url: '/pages/person/person',
          })
        },1000)
      }).catch(res=>{
        console.log('提交订单失败！',res);
      })
  },
  // 获取用户的备注
  getRemark(e){
    console.log('备注：',e.detail.value);
    this.setData({
      beizhu:e.detail.value
    })
  },
 
})